/**
@author		Jong Min, Lee
@date		  01/21/2014
@breif		worklist class for testing

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#include "stdafx.h"
#include "Taurus_Worklist/ToyWorklist.h"

#include "Taurus_Common/IAppGlobalInstanceSet.h"
#include "Taurus_Common/IParameter.h"
#include "Taurus_Common/Monitor.h"
#include "Taurus_Common/SystemEnvironment.h"
#include "Taurus_Xml/XmlFunc.h"

#include "Taurus_Worklist/ToyWorklistDlg.h"

IAppGlobalInstanceSet* CToyWorklist::m_pGInstanceSet = NULL;

CToyWorklist::CToyWorklist()
{
  AfxEnableControlContainer();
  m_pWorklistDlg = NULL;
}

CToyWorklist::~CToyWorklist()
{
  destroy();
}

DWORD CToyWorklist::init(IAppGlobalInstanceSet* pGSet)
{
  m_pGInstanceSet = pGSet;

  m_pWorklistDlg = new CToyWorklistDlg;

  m_pWorklistDlg->Create(CToyWorklistDlg::IDD, NULL);

  return ERROR_SUCCESS;
}

DWORD CToyWorklist::destroy()
{
  if(m_pWorklistDlg)
  {
    m_pWorklistDlg->DestroyWindow();
    delete m_pWorklistDlg;
  }

  return ERROR_SUCCESS;
}

DWORD CToyWorklist::commitViewerProperty(IParameter* pParam)
{
  if(pParam == NULL)
    return ERROR_INVALID_PARAMETER;

  CString strPart;
  pParam->getParam(PARAM_USER_PROFILE_PART,
                   reinterpret_cast<WPARAM>(&strPart),
                   NULL);

  CString strItem;
  pParam->getParam(PARAM_USER_PROFILE_ITEM,
                   reinterpret_cast<WPARAM>(&strItem),
                   NULL);

  if(strPart == _T("USER_PROFILE"))
  {
    IAppGlobalInstanceSet* pAGSet = getAppGlobalInstanceSet();
    CSystemEnvironment* pSysEnv = pAGSet->getSystemEnvironment();

    if(strItem == _T("VIEWER_PROFILE"))
    {
      CAdapt<MSXML2::IXMLDOMDocument2Ptr> cProfileValue;
      pParam->getParam(PARAM_USER_PROFILE_VALUE,
                       reinterpret_cast<WPARAM>(&cProfileValue),
                       NULL);

      CString strPropertyPath = pSysEnv->getPropertyPath();
      strPropertyPath += _T("viewer_property.xml");

      XmlFunc::SaveXmlDoc(cProfileValue.m_T, strPropertyPath);
    }
  }
  return ERROR_SUCCESS;
}

DWORD CToyWorklist::showWorklist(const CMonitor* pMonitor, BOOL bRepaint)
{
  if(pMonitor == NULL)
    return ERROR_INVALID_PARAMETER;

  const RECT* prcMonitor = pMonitor->getWorkRect();

  UINT nFlag = SWP_SHOWWINDOW;
  if(!bRepaint)
    nFlag = SWP_NOREDRAW;

  m_pWorklistDlg->SetWindowPos(NULL,
                               prcMonitor->left,
                               prcMonitor->top,
                               prcMonitor->right - prcMonitor->left,
                               prcMonitor->bottom - prcMonitor->top,
                               nFlag);
  return ERROR_SUCCESS;
}

IAppGlobalInstanceSet* CToyWorklist::getAppGlobalInstanceSet()
{
  return m_pGInstanceSet;
}
